export function hidePhoneNumber(phone) {
  if (!phone) {
    return ''
  }
  if (phone.length >= 7) {
    return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
  }
  return phone
}

export function hideIdCardNumber(idCard) {
  if (!idCard) {
    return ''
  }
  if (idCard.length === 15) {
    return idCard.replace(/(\d{6})\d{6}(\d{3})/, '$1******$2')
  } else if (idCard.length === 18) {
    return idCard.replace(/(\d{6})\d{8}(\d{4})/, '$1********$2')
  }
  return idCard
}

export function buildUrl(url) {
  if (!url) {
    return ''
  }
  if (url.includes('http:') || url.includes('https:')) {
    return url
  }
  if (url.includes('blob:')) {
    return ''
  } else {
    return import.meta.env.VITE_APP_API_BASEURL + url
  }
}

export function parseJsonObject(value) {
  // return value && value !== null && Array.isArray(value)
  // return typeof value === 'object' && value !== null && !Array.isArray(value)
  if (!value || value === null) {
    return value
  } else if (Array.isArray(value)) {
    return value
  } else if (typeof value === 'object') {
    return value
  } else {
    return JSON.parse(value)
  }
}

export function sortJsonObject(obj) {
  // 排序顺序将是：数字、大写字母、下划线、小写字母
  const newObj = parseJsonObject(obj)
  const sortedObj = {}
  const sortedKeys = Object.keys(newObj).sort()
  sortedKeys.forEach((key) => {
    if (newObj[key] != undefined && newObj[key] != null) {
      if (typeof newObj[key] === 'object' || Array.isArray(newObj[key])) {
        sortedObj[key] = newObj[key]
      } else {
        sortedObj[key] = newObj[key].toString()
      }
    }
  })
  return sortedObj
}

export function getDatetime() {
  const now = new Date()
  const year = now.getFullYear()
  const month = String(now.getMonth() + 1).padStart(2, '0')
  const day = String(now.getDate()).padStart(2, '0')
  const hours = String(now.getHours()).padStart(2, '0')
  const minutes = String(now.getMinutes()).padStart(2, '0')
  const seconds = String(now.getSeconds()).padStart(2, '0')

  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

export function getDate() {
  const now = new Date()
  const year = now.getFullYear()
  const month = String(now.getMonth() + 1).padStart(2, '0')
  const day = String(now.getDate()).padStart(2, '0')

  return `${year}-${month}-${day}`
}

export function getDate2() {
  const now = new Date()
  const year = now.getFullYear()
  const month = String(now.getMonth() + 1).padStart(2, '0')
  const day = String(now.getDate()).padStart(2, '0')

  return `${year}${month}${day}`
}

// 使用Map高效合并数组对象
export function mergeArraysBykey(arr1, arr2, key) {
  const map = new Map()

  // 将第一个数组加入Map
  arr1.forEach((item) => map.set(item[key], { ...item }))

  // 合并第二个数组
  arr2.forEach((item) => {
    if (map.has(item[key])) {
      // 合并相同key的对象（后者覆盖前者）
      map.set(item[key], { ...map.get(item[key]), ...item })
    } else {
      // 新增不存在key的对象
      map.set(item[key], { ...item })
    }
  })

  // 将Map的值转为数组
  return Array.from(map.values())
}
